package org.example.corpus.corpuscenter.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.example.corpus.common.ResponseResult;
import org.example.corpus.corpuscenter.service.CorpusService;
import org.example.corpus.model.Corpus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.expression.spel.ast.BooleanLiteral;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

@RestController
@RequestMapping("/corpus")
@Slf4j
public class CorpusCenterController {

    @Autowired
    private CorpusService corpusService;

    // 预览可用语料库列表
    @GetMapping("/list")
    public ResponseResult listCorpora(@RequestParam(defaultValue = "1") int pageNum,
                                      @RequestParam(defaultValue = "10") int pageSize,
                                      @RequestParam(required = false) String keyword,
                                      @RequestParam(required = false,defaultValue = "false")Boolean isUser) {
        int offset = (pageNum - 1) * pageSize;
        List<Corpus> corpusList = corpusService.getCorpusList(offset, pageSize, keyword, isUser);

        return ResponseResult.success(corpusList,"获取成功");
    }



    // 下载现有语料库
    @GetMapping("/download")
    public ResponseEntity downloadCorpus(@RequestParam int corpusId) throws IOException {
        // 获取资源
        Resource resource = corpusService.downloadCorpus(corpusId);
        String fileName = corpusService.getCorpusNameById(corpusId);
        ResponseResult responseResult;

        if (resource != null) {
            String contentType = "application/octet-stream";  // 默认的文件类型

            // 尝试获取文件类型
            try {
                contentType = Files.probeContentType(resource.getFile().toPath());
            } catch (IOException e) {
                // 处理异常日志
                // log.error("无法获取文件类型", e);
            }
            // 返回文件流
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(contentType)) // 设置内容类型
                    .header(HttpHeaders.CONTENT_DISPOSITION, fileName) // 设置文件下载时的文件名
                    .body(new InputStreamResource(resource.getInputStream())); // 返回文件流
        }

        // 如果资源不存在
        responseResult = ResponseResult.error("语料库不存在");
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseResult);
    }



    // 创建用户语料库
    @PostMapping("/usercopus")
    public ResponseResult createUserCorpus(@RequestParam String name,
                                              @RequestParam String description,
                                              @RequestParam("file") MultipartFile file) {
        Integer ret = corpusService.createCorpus(name,description,file);
        if(ret==1)
            return ResponseResult.success();
        else
            return ResponseResult.error("创建失败");
    }

    @GetMapping("/select")
    public ResponseResult selectCorpus(@RequestParam String keyword) {
        List<Corpus> corpusList = corpusService.selectCorpusList(keyword);
        return ResponseResult.success(corpusList,"获取成功");
    }

}
